package com.example.jdbconectiontest.config;

import com.mysql.cj.jdbc.MysqlConnectionPoolDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.sql.SQLException;

@Configuration
public class MysqlConfig {
    @Value("${mysql.ip}")
    String mysqlIp;
    @Value("${mysql.port}")
    Integer mysqlPort;
    @Value("${mysql.password}")
    String mysqlPwd;
    @Value("${mysql.user}")
    String mysqlUser;
    @Value("${mysql.db}")
    String mysqlDB;

    @Bean
    public MysqlConnectionPoolDataSource mysqlPooledConnection() throws SQLException {
        MysqlConnectionPoolDataSource mysqlConnectionPoolDataSource = new MysqlConnectionPoolDataSource();
        mysqlConnectionPoolDataSource.setServerName(mysqlIp);
        mysqlConnectionPoolDataSource.setPort(mysqlPort);
        mysqlConnectionPoolDataSource.setPassword(mysqlPwd);
        mysqlConnectionPoolDataSource.setUser(mysqlUser);
        mysqlConnectionPoolDataSource.setDatabaseName(mysqlDB);
        mysqlConnectionPoolDataSource.setMaxReconnects(10);
        mysqlConnectionPoolDataSource.setAutoReconnectForPools(true);
        mysqlConnectionPoolDataSource.setAllowMultiQueries(true);
        return mysqlConnectionPoolDataSource;
    }


}
